import GitHub from 'next-auth/providers/github';
import { DrizzleAdapter } from '@auth/drizzle-adapter';
import { db } from '../db/db';
import { accounts, sessions, users, verificationTokens } from '../db/schema';
import type { AuthOptions, DefaultSession } from 'next-auth';
import { getServerSession } from 'next-auth';
import { githubAuthProviderConfig } from '@/constants/config';

declare module 'next-auth' {
  interface Session extends DefaultSession {
    user: {
      id: string;
    } & DefaultSession['user'];
  }
}

export const authOptions: AuthOptions = {
  adapter: DrizzleAdapter(db, {
    usersTable: users,
    accountsTable: accounts,
    sessionsTable: sessions,
    verificationTokensTable: verificationTokens,
  }),
  callbacks: {
    async session({ session, user }) {
      if (session.user && user) {
        session.user.id = user.id;
      }

      return session;
    },
  },
  providers: [GitHub(githubAuthProviderConfig)],
};

export async function getUserServerSession() {
  return getServerSession(authOptions);
}
